<?php

namespace App\Http\Middleware;

use Closure;

class Language
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $default_language = "zh-CN";
        $support_language = ["zh-CN", "en"];
        if ($request->hasHeader('Accept-Language')) {
            $expect_languages = $request->getLanguages();
            foreach ($expect_languages as $key => $value) {
                if (in_array($value, $support_language)) {
                    $default_language = $value;
                }
            }
        }
        $request->setLocale($default_language);
        return $next($request);
    }
}
